package com.xfzp.android.ms;

import com.xfzp.d3.Color;
import com.xfzp.d3.Cube;
import com.xfzp.d3.Shape;
import com.xfzp.d3.Vertex;

/**
 * 
 * @author AndroidApe
 * @version 2012-11-2
 */
public class InitKube {
	public static final float cubeWidth = 0.63f;
	public static final float spaceWidth = 0.055f;

	/**
	 * 初始化立方体
	 */
	public static Shape[] initCubes() {
		return initColor(initCube(initScale()));
	}

	/**
	 * 初始化每个轴上的6个刻度值
	 * 
	 * @return
	 */
	private static float[] initScale() {
		float[] scale = new float[6];
		scale[3] = cubeWidth / 2;
		scale[2] = -scale[3];
		scale[1] = scale[2] - spaceWidth;
		scale[0] = scale[1] - cubeWidth;
		scale[4] = scale[3] + spaceWidth;
		scale[5] = scale[4] + cubeWidth;
		return scale;
	}

	/**
	 * 初始化小立方体。小立方体在World中的索引顺序按从后到前，从下到上，从左到右
	 * 
	 * @param scale
	 * @return
	 */
	private static Shape[] initCube(float[] scale) {
		Shape[] cs = new Shape[26];
		int ci = 0;
		int t1, t2, t3;
		for (int k = 0; k < 6; k++) {
			t3 = k++;
			for (int j = 0; j < 6; j++) {
				t2 = j++;
				for (int i = 0; i < 6; i++) {
					t1 = i++;
					if (t3 == 2 && t2 == 2 && t1 == 2) {
						continue;
					}
					cs[ci++] = new Cube(new Vertex(scale[t1], scale[t2],
							scale[t3]),
							new Vertex(scale[i], scale[j], scale[k]));
				}
			}
		}
		return cs;
	}

	/**
	 * 着色
	 * 
	 * @param cs
	 * @return
	 */
	private static Shape[] initColor(Shape[] cs) {
		Shape c;
		int i2;
		for (int i = 0; i < 26; i++) {
			c = cs[i];
			i2 = i % 3;
			c.set(Color.BALCK, 0, 1, 2, 3, 4, 5);
			if (i < 13) {
				if (i < 9) {
					c.set(Color.ORANGE, 0);
				}
				if (i2 == 0) {
					c.set(Color.BLUE, 1);
				} else if (i2 == 2) {
					c.set(Color.GREEN, 3);
				}
				if (i < 3 || (i > 8 && i < 12)) {
					c.set(Color.YELLOW, 4);
				} else if (i > 5 && i < 9) {
					c.set(Color.WHITE, 2);
				}
			} else {
				if (i > 16) {
					c.set(Color.RED, 5);
				}
				if (i2 == 2) {
					c.set(Color.BLUE, 1);
				} else if (i2 == 1) {
					c.set(Color.GREEN, 3);
				}
				if (i > 16 && i < 20) {
					c.set(Color.YELLOW, 4);
				} else if ((i > 13 && i < 17) || i > 22) {
					c.set(Color.WHITE, 2);
				}
			}
		}
		return cs;
	}

}
